package com.kkb.shopcenter.server.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.shopcenter.api.BusinessService;
import com.kkb.shopcenter.common.pojo.BusinessInfo;
import com.kkb.shopcenter.common.pojo.ShopInfo;
import com.kkb.shopcenter.common.pojo.UserInfo;
import com.kkb.shopcenter.server.mappers.BusinessInfoMapper;
import com.kkb.shopcenter.server.mappers.ShopInfoMapper;
import com.kkb.shopcenter.server.mappers.UserInfoMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BusinessServiceImpl implements BusinessService {


    @Autowired
    private BusinessInfoMapper businessInfoMapper;

    @Autowired
    private ShopInfoMapper shopInfoMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public PageInfo<BusinessInfo> getBusinessList(Integer pageNo, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNo, pageSize);
        Example example = new Example(BusinessInfo.class);
        if(status>=0){
            example.createCriteria().andEqualTo("status",status);
        }
        List<BusinessInfo> businessInfos = businessInfoMapper.selectByExample(example);
        PageInfo<BusinessInfo> pageInfo = new PageInfo<>(businessInfos);
        return  pageInfo;
    }

    /**
     * 判断是否存在同名商家名称
     * @param compName 商家名单
     * @return
     */
    @Override
    public int selectCompNameExit(String compName) {
        Example example = new Example(BusinessInfo.class);
        example.createCriteria().andEqualTo("compName",compName);
        return businessInfoMapper.selectCountByExample(example);
    }

    /**
     * 判断是否存在同名的店铺名称
     * @param shopName
     * @return
     */
    @Override
    public int shopNameIsExist(String shopName) {
        Example example = new Example(ShopInfo.class);
        example.createCriteria().andEqualTo("shopName",shopName);
        return shopInfoMapper.selectCountByExample(example);

    }

    /**
     * 判断是否存在同名用户名称
     * @param userName
     * @return
     */
    @Override
    public int shopUserNameIsExist(String userName) {
        Example example = new Example(UserInfo.class);
        example.createCriteria().andEqualTo("userName",userName);
        return userInfoMapper.selectCountByExample(example);

    }

    @Override
    public int register(BusinessInfo businessInfo) {

        //添加商家信息
       int result =  businessInfoMapper.insertSelective(businessInfo);

        //获取最新的主键ID，也就是最新的商家ID
        Long businessId = businessInfo.getId();

        ShopInfo shopInfo = businessInfo.getShopInfo();
        //设置商家ID
        shopInfo.setBusinessId(businessId);

        //添加店铺信息
        shopInfoMapper.insertSelective(shopInfo);

        Long shopId = shopInfo.getId();


        UserInfo userInfo = businessInfo.getUserInfo();

        //设置用户的店铺ID
        userInfo.setShopId(shopId);

        //加密用户密码
        userInfo.setUserPassword(new BCryptPasswordEncoder().encode(userInfo.getUserPassword()));

        userInfoMapper.insertSelective(userInfo);

        return result;
    }
}
